#include "answerform.h"
#include "ui_answerform.h"
#include "SqliteDb.h"
#include <QDir>
#include <QMessageBox>
#include <QRandomGenerator>
#include "dialog.h"

AnswerForm::AnswerForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AnswerForm)
{
    ui->setupUi(this);
}

AnswerForm::~AnswerForm()
{
    delete ui;
}

void AnswerForm::Init()
{
    int maxId = SqliteDb::m_SqliteDb.getMaxId("data");
    for(int i=1; i<=maxId; i++)
    {
        QString str = QString("第%1道题").arg(i);
        ui->comboBox->addItem(str);
    }

    QDir dir(QDir::currentPath() + "/Resource/postcard");
    if (!dir.exists())
    {
        return;
    }
    dir.setFilter(QDir::Files | QDir::NoSymLinks);
    QStringList filters;
    filters << "*.jpg" << "*.png";
    dir.setNameFilters(filters);
    m_strPostCardList = dir.entryList();
    on_comboBox_currentIndexChanged(0);
}

void AnswerForm::on_btn_submit_clicked()
{
    if(ui->textEdit->toPlainText() != m_strResult)
    {
        QMessageBox::warning(this, "警告", "您的答案错误！");
        return;
    }
    QMessageBox::information(this, "提示", "您的答案正确！");
    int i = QRandomGenerator::global()->bounded(m_strPostCardList.size() - 1);
    Dialog dialog(m_strPostCardList[i]);
    dialog.exec();
}

void AnswerForm::on_comboBox_currentIndexChanged(int index)
{
    int id = index + 1;
    DB_TYPE_DATA dataMap;
    DB_TYPE_DATA filterMap;
    filterMap["id"] = id;
    SqliteDb::m_SqliteDb.getRow("data", dataMap, filterMap);
    QString strPath = QDir::currentPath() + "/Resource/Topic/" + dataMap["name"].toString();
    QPixmap pixMap(strPath);
    pixMap = pixMap.scaled(1200, 580);
    ui->label_2->setPixmap(pixMap);
    m_strResult = dataMap["result"].toString();
}
